package com.suyafei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suyafei.enums.AddressStatusEnum;
import com.suyafei.interceptor.LoginIntercept;
import com.suyafei.model.AddressDO;
import com.suyafei.mapper.AddressMapper;
import com.suyafei.model.LoginUser;
import com.suyafei.request.AddressAddRequest;
import com.suyafei.service.AddressService;
import com.suyafei.vo.AddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.mail.Address;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电商-公司收发货地址表 服务实现类
 * </p>
 *
 * @author suyafei
 * @since 2021-01-28
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVO detail(Long id) {
        LoginUser loginUser = LoginIntercept.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id).eq("user_id", loginUser.getId()));

        if (addressDO == null) {
            return null;
        }
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO, addressVO);

        return addressVO;
    }

    /**
     * 新增收货地址
     * @param addressAddRequest
     */
    @Override
    public void add(AddressAddRequest addressAddRequest) {
        LoginUser loginUser = LoginIntercept.threadLocal.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());

        BeanUtils.copyProperties(addressAddRequest,addressDO);

        //是否有默认收货地址
        if (addressDO.getDefaultStatus() == AddressStatusEnum.DEFAULT_STATUS.getStatus()) {
            //查找数据库是否有默认地址
            AddressDO defaultAddressDO = addressMapper
                    .selectOne(new QueryWrapper<AddressDO>()
                            .eq("user_id", loginUser.getId())
                            .eq("default_status", AddressStatusEnum.DEFAULT_STATUS.getStatus()));
            if (defaultAddressDO != null) {
                defaultAddressDO.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
                addressMapper.update(defaultAddressDO,
                        new QueryWrapper<AddressDO>()
                                .eq("id", defaultAddressDO.getId()));
            }
        }
        int rows = addressMapper.insert(addressDO);

        log.info("新怎收货地址：rows={},data={}", rows, addressDO);
    }

    /**
     *根据Id删除收货地址
     * @param addressId
     * @return
     */
    @Override
    public int del(long addressId) {
        LoginUser loginUser = LoginIntercept.threadLocal.get();
        int rows = addressMapper.delete(new QueryWrapper<AddressDO>().eq("id", addressId).eq("user_id",loginUser.getId()));
        return rows;
    }

    /**
     * 查找所有的收货地址
     *
     * @return
     */
    @Override
    public List<AddressVO> listUserAllAddress() {
        LoginUser loginUser = LoginIntercept.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));

        List<AddressVO> addressVOList = addressDOList.stream().map(obj -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj, addressVO);
            return addressVO;
        }).collect(Collectors.toList());

        return addressVOList;
    }

}
